package com.piece.core.framework.exception;

import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.util.basic.I18nUtil;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
 * 错误码
 */
@Getter
public class ErrorCode {

    // 系统异常
    public static ErrorCode RUNTIME = new ErrorCode(500, ExceptionConstants.RUNTIME, null);
    // 服务器异常
    public static ErrorCode SERVER = new ErrorCode(500, ExceptionConstants.SERVER, null);
    // Http请求异常
    public static ErrorCode HTTP = new ErrorCode(500, ExceptionConstants.HTTP, null);
    // 参数解析异常
    public static ErrorCode ARGUMENT = new ErrorCode(400, ExceptionConstants.ARGUMENT, null);
    // 访问限制
    public static ErrorCode ACCESS = new ErrorCode(403, ExceptionConstants.ACCESS, null);
    // 权限限制
    public static ErrorCode PERMISSION = new ErrorCode(403, ExceptionConstants.PERMISSION, null);
    // 请求未找到
    public static ErrorCode NOT_FOUND = new ErrorCode(404, ExceptionConstants.NOT_FOUND, null);
    // 获取锁异常
    public static ErrorCode LOCK = new ErrorCode(423, ExceptionConstants.LOCK, null);
    // 幂等性异常
    public static ErrorCode IDEMPOTENCY = new ErrorCode(423, ExceptionConstants.IDEMPOTENCY, null);
    // 频繁请求限制
    public static ErrorCode REQUEST_FREQUENTLY = new ErrorCode(429, ExceptionConstants.REQUEST_FREQUENTLY, null);
    // 上传文件异常
    public static ErrorCode UPLOAD = new ErrorCode(500, ExceptionConstants.UPLOAD_ERROR, null);
    // 下载文件异常
    public static ErrorCode DOWNLOAD = new ErrorCode(500, ExceptionConstants.DOWNLOAD_ERROR, null);
    // 导出文件异常
    public static ErrorCode EXPORT = new ErrorCode(500, ExceptionConstants.EXPORT, null);
    // 保存异常
    public static ErrorCode SAVE = new ErrorCode(500, ExceptionConstants.SAVE_FAILURE, null);
    // 删除异常
    public static ErrorCode DELETE = new ErrorCode(500, ExceptionConstants.DELETE_FAILURE, null);
    // 演示模式限制
    public static ErrorCode DEMO = new ErrorCode(900, ExceptionConstants.DEMO, null);
    // 正常
    public static ErrorCode SUCCESS = new ErrorCode(200, ExceptionConstants.MESSAGE_SUCCESS, null);

    /**
     * 错误码
     */
    private Integer code;
    /**
     * 国际化code
     */
    private String key;
    /**
     * 错误码对应的参数
     */
    private Object[] args;
    /**
     * 错误提示
     */
    private String msg;

    public ErrorCode(String msg) {
        this.code = 500;
        this.msg = msg;
    }

    public ErrorCode(String key, String args) {
        this(key, new Object[]{args});
    }

    public ErrorCode(String key, Object[] args) {
        this.code = 500;
        this.key = key;
        this.args = args;
        this.convertMsg();
    }

    public ErrorCode(Integer code, String key, Object[] args) {
        this.code = code;
        this.key = key;
        this.args = args;
        this.convertMsg();
    }

    public ErrorCode(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public void convertMsg() {
        if (StringUtils.isNotEmpty(this.key)) {
            this.msg = I18nUtil.message(this.key, this.args);
        }
    }
}
